package com.ihd.ihardware.view.enter.viewModel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemLockBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.enter.model.HomeRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class DevicesViewModel extends AndroidViewModel {

    private HomeRepository mMainRepository;

    public DevicesViewModel(@NonNull Application application) {
        super(application);
    }

    public void unbind(String id) {
        if (mMainRepository == null)
            mMainRepository = new HomeRepository();
        mMainRepository.unbind(id);
    }

    private ObservableArrayList<DeviceListRes.DataBean.ListBean> mMembers = new ObservableArrayList<>();
    public CommonAdapter mZWAdapter = new CommonAdapter<DeviceListRes.DataBean.ListBean, ItemLockBinding>(R.layout.item_lock, mMembers, null) {
        @Override
        public void convert(ItemLockBinding mDataBinding, DeviceListRes.DataBean.ListBean test_concern, int position) {
            mDataBinding.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    unbind(test_concern.getId());
                }
            });
            mDataBinding.itemLL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    };

    public void setMembers(List<DeviceListRes.DataBean.ListBean> list) {
        mMembers.clear();
        if (list != null && list.size() > 0)
            mMembers.addAll(list);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mMainRepository) {
            mMainRepository.onDestroy();
            mMainRepository = null;
        }
    }
}
